
let input = {
    mounted(el, binding, vnode) {
        let preVal = null;//按下键盘前的value
        el.handler = function (e) {
            //dataType=0 数字类型,dataType=1 字符串类型   
            
                if(binding.value.field.dataType==0){
                    /* 不合范围的数字直接不给输入 */
                if(binding.value.field.dataMaxLength<=Number(e.target.value)||Number(e.target.value)<binding.value.field.dataMinLength){
                    e.target.value = preVal.replace(/^\-*\+*$/,'')
                    e.target.dispatchEvent(new Event("input"));
                }
                /* if(binding.value.field.zero==1&&Number(e.target.value)===0){
                    e.target.value = preVal
                    e.target.dispatchEvent(new Event("input"));
                } */
                if(/^\d*\.*\d+[eE]{0,1}[+-]?\d*$/.test(e.target.value)){
                   return
                 }
                let regexp = '^\-?\\d*[eE]{0,1}[+-]?(\\.?\\d{0,})';
                
                e.target.value = (e.target.value.match(new RegExp(regexp, 'g'))[0]) || '';
            }else if(binding.value.field.dataType==1){
                if(!e.target.value){return}
                e.target.value = e.target.value.replace(/[\u4e00-\u9fa5]/gi,'') //(e.target.value.match(/[^\u4e00-\u9fa5]/gi)[0]) || '';
            }
            e.target.dispatchEvent(new Event("input")); //调用input事件使vue v-model绑定更新,下面相同
        };
        //记录上次输入的值
        const recordPreval = (e)=>{
            preVal = e.target.value
        }
        const clearInvalidNote = (e)=>{
            if(isNaN(Number(e.target.value))){
                e.target.value = ''
            }else{
                e.target.value = Number(e.target.value)
            }
            e.target.dispatchEvent(new Event("input"));
        }
        el.addEventListener('keydown', recordPreval);
        el.addEventListener('keyup', el.handler);
        if(binding.value.field.dataType==0){
            el.addEventListener('change', clearInvalidNote);
        }
        
  }
}
export default input
